package com.dissertation.service.impl;

import com.dissertation.annotation.AdminAuth;
import com.dissertation.model.Assets;
import com.dissertation.pojo.Message;
import com.dissertation.service.AssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

@Service
public class AssetsImpl implements AssetsService {
    @Autowired
    MongoTemplate template;

    @AdminAuth
    @Override
    public Message deleteAssets(List<String> idList) {
        Query query = Query.query(where("id").in(idList).and("deleted").is(false));
        Update update = Update.update("deleted", true);
        int number = template.updateMulti(query, update, Assets.class).getN();
        return Message.createSuc(number);
    }

    @AdminAuth
    @Override
    public Message recoverAssets(List<String> idList) {
        Query query = Query.query(where("id").in(idList).and("deleted").is(true));
        Update update = Update.update("deleted", false);
        int number = template.updateMulti(query, update, Assets.class).getN();
        return Message.createSuc(number);
    }

    @Override
    public List<Assets> findByAuthorAndType(String name, String categoryLeafName) {
        Query query = Query.query(where("author").is(name).and("deleted").is(false).and("categoryLeafName").is(categoryLeafName));
        return template.find(query,Assets.class);
    }
}
